// pages/goods_order_confirm/goods_order_confirm.js
var DateUtil = require('../../../utils/DateUtil.js');
var wxPay = require('../../../utils/wxPay.js');
var app = getApp();
var order_id;
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    order_id = options.order_id;
    this.getOrderInfo();
  },

  /**
   * 获取订单详情
   */
  getOrderInfo: function () {
    app.Ajax({
      url: '/api/index.php?app=order&act=detail',
      data: {
        order_id: order_id,
      },
      success: (res) => {
        // console.log(res)
        //订单状态
        var order_status = res.status;
        //截止时间
        var end_time = res.end_time.toString();
        end_time = end_time.replace(/-/g, "/");
        end_time = new Date(end_time);
        var remainTime = DateUtil.getExpectedTime2(end_time.getTime());

        //地址信息
        var address_info = {};
        address_info.consignee = res.consignee;
        address_info.phone_mob = res.phone_mob;
        address_info.address = res.region_name + ' ' + res.address;

        //商品信息
        var goods_info = res.goods[0];
        var goods_attr = goods_info.sku_attr;
        var goods_attr_arr = goods_attr.split(';');
        goods_info.goods_attr_arr = goods_attr_arr;


        this.setData({
          order_status: order_status,
          remainTime: remainTime,
          address_info: address_info,
          goods_info: goods_info,
          stars: res.stars,
          shipping_fee: res.shipping_fee,
          pay_amount: res.pay_amount
        })
      }
    })
  },

  /**
   * 取消订单
   */
  orderCancel: function() {
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=order&act=cancel',
      data: {
        order_id: order_id,
      },
      success: (res) => {
        this.setData({
          message: '订单已取消'
        })
        setTimeout(() => {
          wx.switchTab({
            url: '../../homepage/homepage',
          })
        }, 2000);
      }
    })
  },

  /**
   * 付款
   */
  orderSubmit: function() {
    wxPay.wxPayMoneyByOrderid(order_id, (succ) => {
      wx.redirectTo({
        url: '../../my/myorder/myorder?order_type=20',
      })
    }, (fail) => {
      this.setData({
        message: '支付失败'
      })
    })
  }
})